package cn.sharp.bbs.controller;


import cn.sharp.bbs.common.Result;
import cn.sharp.bbs.common.TokenUtils;
import cn.sharp.bbs.entity.BO.CurrentUser;
import cn.sharp.bbs.entity.DO.Report;
import cn.sharp.bbs.entity.DO.ReportDto;
import cn.sharp.bbs.service.CommonService;
import cn.sharp.bbs.service.ReportService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author zhao yu
 * @version 0.0.2
 */
@RestController
@RequestMapping("/report")
public class ReportController {

    @Resource
    private ReportService reportService;

    @Resource
    private CommonService commonService;

    /**
     * 获得原因列表
     * @param type 举报对象类型
     * @return 原因列表的封装
     */
    @GetMapping("/getReasonList/{type}")
    public Result getReasonList(@PathVariable("type") int type){
        return Result.success(reportService.getReasonList(type));
    }

    /**
     * 用户提交举报
     * @param token 令牌
     * @param record 举报记录
     */
    @PostMapping("/commit")
    public Result report(@RequestHeader("Token") String token, @RequestBody Report record) {

        if(record.getType() < 0 || record.getType() > 2) {
            return Result.fail(Result.ERR_CODE_BUSINESS, "举报类型错误！请修改请求");
        }
        if(!reportService.checkID(record.getType(), record.getTarget())) {
            return Result.fail(Result.ERR_CODE_BUSINESS, "该类型举报对象不存在id: " + record.getTarget());
        }

        CurrentUser currentUser = TokenUtils.getUserInfo(token, commonService);
        record.setsUserId(currentUser.getU_id());
        record.setState(0);
        reportService.report(record);
        return Result.success();
    }

    /**
     * 后台获得举报列表
     */
    @GetMapping("/list")
    public Result getReportList() {
        return Result.success(reportService.getReportList());
    }

    /**
     * 举报审核通过
     * @param id 举报记录的id
     * @param user 处理举报的管理员
     */
    @PutMapping("/pass")
    public Result pass(@RequestBody ReportDto reportDto,
                       @RequestAttribute("CurrentUser") CurrentUser user){
        reportDto.setHUserId(user.getU_id());
        reportService.pass(reportDto);
        return Result.success();
    }

    /**
     * 举报审核通过
     * @param id 举报记录的id
     * @param user 处理举报的管理员
     */
    @PutMapping("/reject/{id}")
    public Result reject(@PathVariable("id") Long id,
                       @RequestAttribute("CurrentUser") CurrentUser user){
        reportService.reject(id, user);
        return Result.success();
    }
}
